using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : Character
{
    public int form = 0; //0复合  1紫色形态 2红色形态
    private int changeForm; //接下来要变的形态

    public float runTimeInterval = 1; //两次方向键时间间隔
    private float upDownTime;           //上一次按键时间
    private float upDir;                //上一次按键方向

    


    // Start is called before the first frame update
    new void Start()
    {
        base.Start();
    }

    // Update is called once per frame
    new void Update()
    {
        base.Update();
        Walk();

 


        Running();
        ReadyJump();
        CommonAttack();
        RayAttack();
        skillAttack();
        Change();
        Anim();
    }

    new void FixedUpdate()
    {
        base.FixedUpdate();
        //只有这些状态才可进行移动，获取键盘指令
        ClipCanMove(new string[] { "idle", "walk", "run", "jump", "jump_fall" });

    }

    private void skillAttack()
    {
        if (Input.GetButtonDown("Skill"))
        {
            childAnimator.SetTrigger("Skill");
        }
    }
    private void CommonAttack()
    {
        if (Input.GetButtonDown("Common Attack"))
        {
            childAnimator.SetTrigger("Attack");

        }
    }

    private void RayAttack()
    {
        //光线技能
        if (Input.GetButtonDown("Ray"))
        {
            childAnimator.SetTrigger("Ray");

        }
        //手掌光箭
        if(Input.GetButtonDown("Light"))
        {
            childAnimator.SetTrigger("Light");
        }

        if (Input.GetButtonDown("Ray") && Input.GetAxisRaw("Vertical") < 0)
        {
            childAnimator.SetTrigger("RayTwo");
        }
    }

    private void ReadyJump()
    {
        if (Input.GetButtonDown("Jump") && !isJump && canJump)
        {
            isJump = true;
            //获取跳跃初速度
            velocity_Y = Mathf.Sqrt(jumpHeight * -2f * gSpeed);
        }     
    }



    private void Running()
    {

        if (Input.GetButtonDown("Horizontal"))
        {
            if ((Time.time - upDownTime < runTimeInterval) && upDir == inputMove.x && upDir != 0)
            {
                isRun = true;
            }
        }
        //如果上次的方向键不同
        if (Input.GetButtonUp("Horizontal") || upDir != inputMove.x)
        {
            isRun = false;
        }
        //拿到上次的方向键
        if (Input.GetButtonDown("Horizontal"))
        {
            upDownTime = Time.time;
            upDir = inputMove.x;
        }
    }

    private void Change()
    {
        
        if (Input.GetButtonDown("Change Purple"))
        {
            childAnimator.SetTrigger("change");
            changeForm = 1;
            if(form == 1)
            {
                changeForm = 0;
            }
        }
        if (Input.GetButtonDown("Change Red"))
        {
            childAnimator.SetTrigger("change");
            changeForm = 2;
            if (form == 2)
            {
                changeForm = 0;
            }
        }

    }


    private void Walk()
    {
        inputMove.x = Input.GetAxisRaw("Horizontal");
        inputMove.y = Input.GetAxisRaw("Vertical");
    }
    private void Anim()
    {
        childAnimator.SetInteger("change_form", changeForm);
        form = childAnimator.GetInteger("form");

    }














}
